package net.spacebeeshooter.main;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFrame;

import net.spacebeeshooter.items.ConfigSeparator;
import net.spacebeeshooter.items.Item;
import net.spacebeeshooter.items.ItemConfigLoader;
import net.spacebeeshooter.items.ParseableType;
import net.spacebeeshooter.items.StringToNumberConverter;

public class SBSItemBuilder {
	
	private static final String ITEM_CONFIG_LOCATION = "cfg/item.cfg";
	private static final String DEFAULT_CONFIG_LOCATION = "cfg/default.cfg";
	private static final ParseableType<?>[] ALLOWED_ITEMVALUE_TYPES = new ParseableType<?>[]
			{
				new ParseableType<Double>(Double.class, new StringToNumberConverter<Double>() {
				@Override
				public Double convert(String input) {
					return Double.valueOf(input);
				}
				}),
				new ParseableType<Float>(Float.class, new StringToNumberConverter<Float>() {
					@Override
					public Float convert(String input) {
						return Float.valueOf(input);
					}
				}),
				new ParseableType<Byte>(Byte.class, new StringToNumberConverter<Byte>() {
					@Override
					public Byte convert(String input) {
						return Byte.valueOf(input);
					}
				}),
				new ParseableType<Long>(Long.class, new StringToNumberConverter<Long>() {
					@Override
					public Long convert(String input) {
						return Long.valueOf(input);
					}
				}),
				new ParseableType<String>(String.class, new StringToNumberConverter<String>() {
					@Override
					public String convert(String input) {
						return input;
					}
				}),
				new ParseableType<Short>(Short.class, new StringToNumberConverter<Short>() {
					@Override
					public Short convert(String input) {
						return Short.valueOf(input);
					}
				}),
				new ParseableType<Integer>(Integer.class, new StringToNumberConverter<Integer>() {
					@Override
					public Integer convert(String input) {
						return Integer.valueOf(input);
					}
				})
		};
	
	public void start() {
		try {
			ItemConfigLoader loader = new ItemConfigLoader(ITEM_CONFIG_LOCATION, DEFAULT_CONFIG_LOCATION, ALLOWED_ITEMVALUE_TYPES, new ConfigSeparator(",", ",", String.valueOf((char) 9)));
			try {
				Item item = loader.loadItemConfiguration();
				JFrame frame = new GUIConfigurator().buildGui(item, DEFAULT_CONFIG_LOCATION);
				frame.setVisible(true);
			} catch (IllegalArgumentException e) {
				// Automatical termination
			}
			
		} catch (FileNotFoundException e) {
			new ErrorGenerator().generateErrorMessage("item.cfg was not found.");
		} catch (IOException e) {
			new ErrorGenerator().generateErrorMessage("Could not load item.cfg");
		}
	}
}
